package com.ewhale.feitengguest.presenter.user.task;

import com.ewhale.feitengguest.api.ApiHelper;
import com.ewhale.feitengguest.dto.TaskCommentDto;
import com.ewhale.feitengguest.dto.TaskDetailsDto;
import com.ewhale.feitengguest.view.user.task.TaskDetailsView;
import com.simga.library.base.IPresenter;
import com.simga.library.http.APIException;

import java.util.List;

public class TaskDetailsPresenter extends IPresenter {

    public void loadTaskDetail(long missionId) {
        mView.showProLoading();
        request(1, ApiHelper.TASK_API.getTaskDetail(missionId), null);
    }

    public void loadComment(long missionId, int pageNum) {
        request(2, ApiHelper.TASK_API.getTaskComment(missionId, pageNum, 10), pageNum);
    }

    public void acceptTask(long missionId) {
        mView.showProLoading();
        request(3, ApiHelper.TASK_API.acceptMission(missionId), null);
    }

    public void cancelTask(long missionId) {
        mView.showProLoading();
        request(4, ApiHelper.TASK_API.cancelTask(missionId), null);
    }

    @Override
    public <T> void onNext(int postCode, T result, Object data) {
        super.onNext(postCode, result, data);
        switch (postCode) {
            case 1:
                ((TaskDetailsView) mView).showTaskDetails((TaskDetailsDto) result);
                break;
            case 2:
                ((TaskDetailsView) mView).showTaskCommentList((List<TaskCommentDto>) result, (int) data);
                break;
            case 3:
                ((TaskDetailsView) mView).acceptTaskSuccess();
                break;
            case 4:
                ((TaskDetailsView) mView).cancelTaskSuccess();
                break;
        }
    }

    @Override
    public void otherException(int postCode, Object data, APIException e) {
        super.otherException(postCode, data, e);
        mView.showErrorMessage(postCode, e.getCode(), e.getDisplayMessage());
    }

}
